/**
 * wind债券分类  业务组件 树形下拉列表结构
 */
import React from 'react';
import { connect } from 'dva';
import PropTypes from 'prop-types';
import { TreeSelect } from 'antd';
import { FormItemLayout } from '../../form/formPub';
import { TreeSelectC } from '../../form';

class WinBondType extends React.PureComponent {
  // props默认值
  static defaultProps = {
    title: 'wind债券类型',
    placeholder: '请选择交易市场',
    helper: '',
    initialValue: undefined, // 默认无初始化值
    onChange: () => {},
    onSelect: () => {},
    onRef: () => {}, // 本组件的引用
    width: '100%',
    rules: undefined, // 默认无校验规则
    formItemLayout: FormItemLayout, // 设置表单默认布局
    allowClear: true, // 默认可删除
    showCheckedStrategy: TreeSelect.SHOW_PARENT, // treeCheckable为true时，SHOW_PARENT生效
    disabled: false, // 默认不禁用
    isMultiple: true, // 默认多选
    labalWrap: false,
  };

  componentDidMount() {
    // 父组件对本组件的引用
    this.props.onRef(this);
  }

  setSelect = (value) => {
    this.state.WinBondTypeC.setSelect(value);
  };

  changeData = () => {
    const {
      globalM: {
        dataDictionary: { CREDITBND_MONITOR_BONDTYPEI = [] },
      },
    } = this.props;
    const arr = [];
    CREDITBND_MONITOR_BONDTYPEI.map((item) => {
      const obj = {};
      const child = [];
      obj.title = item.value;
      obj.value = item.code;
      item.children &&
        item.children.map((childItem) => {
          const childObj = {};
          childObj.title = childItem.value;
          childObj.value = childItem.code;
          child.push(obj);
        });
      obj.children = child;
      arr.push(obj);
    });
    return arr;
  };

  render() {
    const {
      labalWrap,
      styleRow,
      form,
      title,
      onChange,
      onSelect,
      dataIndex,
      initialValue,
      isMultiple,
      placeholder,
      helper,
      width,
      rules,
      formItemLayout,
      allowClear,
      disabled,
      showCheckedStrategy,
      dataSource,
    } = this.props;
    return (
      <TreeSelectC
        labalWrap={labalWrap}
        styleRow={styleRow ? styleRow : null}
        form={this.props.form} // form对象
        title={title} // 显示名称
        dataIndex={dataIndex} // Form识别的Item ID
        dataSource={this.props.dataSource}
        onChange={onChange}
        onSelect={onSelect}
        allowClear={allowClear}
        showCheckedStrategy={showCheckedStrategy == 'SHOW_CHILD' ? TreeSelect.SHOW_CHILD : showCheckedStrategy == 'SHOW_ALL' ? TreeSelect.SHOW_ALL : TreeSelect.SHOW_CHILD}
        isMultiple={isMultiple}
        treeCheckable={true}
        treeDefaultExpandAll={true}
        formItemLayout={formItemLayout}
        initialValue={initialValue}
        onRef={(ref) => {
          this.setState({ WinBondTypeC: ref });
        }}
      />
    );
  }
}

// props类型
WinBondType.propsTypes = {
  form: PropTypes.object, // form对象
  title: PropTypes.string, // 显示名称
  dataIndex: PropTypes.string, // Form识别的Item ID
  initialValue: PropTypes.string, // 初始默认值
  placeholder: PropTypes.string, // 占位符
  onChange: PropTypes.func, // 输入框内容变化时的回调
  onSelect: PropTypes.func, // 输入框内容变化时的回调
  helper: PropTypes.oneOfType([
    // 提示帮助信息
    PropTypes.string,
    PropTypes.element,
  ]),
  showCheckedStrategy: PropTypes.string, // SHOW_PARENT,SHOW_ALL, SHOW_CHILD,  定义选中项回填的方式
  onRef: PropTypes.func, // 本组件的引用（可用来父组件调用内部方法）
  rules: PropTypes.object, // 校验规则
  formItemLayout: PropTypes.object, // 设置表单布局
  allowClear: PropTypes.bool, // 设置是否可删除
  disabled: PropTypes.bool, // 是否禁用
  isMultiple: PropTypes.bool, // 是否多选
  dataSource: PropTypes.array,
};

export default connect(({ globalM, loading }) => ({ globalM, loading }))(WinBondType);
